package algo_lignes_sites.ihm.couche;

import algo_lignes_sites.data.Data_ls;
import algo_lignes_sites.data.force.Force;
import algo_lignes_sites.data.force.ForceDrawable;
import algo_lignes_sites.data.junction.JunctionDER;
import algo_lignes_sites.data.segment.SegmentDER;
import commons.data.I_Data;
import commons.drawing.couche.CoucheBasic;
import commons.logs.Logger;
import commons.params_appli.ParamsAppli;
import java.awt.Color;
import java.util.ArrayList;
import commons.utils.primitive_geom.Point_d;
import commons.utils.primitive_geom.Rect_d;


public class CoucheForceDER extends CoucheBasic {

	public CoucheForceDER (Data_ls data) {
		super("Forces", null, data);
	}

	// Bon c'est pas super optimisé, car à chaque affichage, on re-créée une
	//  floppée de ForceDrawable
	@Override
	public ArrayList getDrawables (Rect_d rectVisu) {

		ArrayList vRes = new ArrayList();

		ArrayList vJunctionsDER = data.getData(I_Data.DataType.JUNCTION_DER_LS, rectVisu);


		for (int i = 0; i < vJunctionsDER.size(); i++) {
			JunctionDER jDER = (JunctionDER) vJunctionsDER.get(i);

			// Les résultantes
			addForceDrawable (vRes, jDER, jDER.getResultForceRespectForme(), 3, ForceDrawable.COLOR_RESULTANTE_FORME);
			addForceDrawable (vRes, jDER, jDER.getForceAttractPosition(), 0.5f, ForceDrawable.COLOR_RESULTANTE_ATTRACTPOS);
			addForceDrawable (vRes, jDER, jDER.getResultForceEntrainement_by_AllNode(), 3, ForceDrawable.COLOR_RESULTANTE_ENTRAINEMENT);
			addForceDrawable (vRes, jDER, jDER.getResultForceRepuls_Seg_Seg(), 3, ForceDrawable.COLOR_RESULTANTE_REPULS);
			addForceDrawable (vRes, jDER, jDER.getResultForceRepuls_Node_Seg(), 5, ForceDrawable.COLOR_RESULTANTE_REPULS);
			
			if (jDER.isNode()) {
				addForceDrawable (vRes, jDER, jDER.getResultForceRepuls_Seg_Node(), 3, ForceDrawable.COLOR_RESULTANTE_REPULS);
				addForceDrawable (vRes, jDER, jDER.getResultForceRepuls_Node_Node(), 5, ForceDrawable.COLOR_RESULTANTE_REPULS);
			}
			
			// Les composantes de repulsion
			if (Logger.isDebugLevel()) {
				if (jDER.nbSeg>=1)
					addForceDrawable (vRes, jDER, jDER.seg1.getForceRepulsSegForThisJunction(jDER), 1, ForceDrawable.COLOR_RESULTANTE_REPULS);
				if (jDER.nbSeg>=2)
					addForceDrawable (vRes, jDER, jDER.seg2.getForceRepulsSegForThisJunction(jDER), 1, ForceDrawable.COLOR_RESULTANTE_REPULS);
				if (jDER.nbSeg>=3) {
				// le reste des segments connectés à this est dans un vecteur
				for (int k = 0; k<jDER.nbSeg-2; k++) {
					SegmentDER seg = jDER.vSeg_autre.get(k);
					addForceDrawable (vRes, jDER, seg.getForceRepulsSegForThisJunction(jDER), 1, ForceDrawable.COLOR_RESULTANTE_REPULS);
				}
				}
		}
			
		}
		return vRes;
	}


	private void addForcesDrawable (ArrayList vRes, Point_d ptDepart, ArrayList<Force> vForces, float width, Color color) {
		for (int i = 0; i < vForces.size(); i++) {
			Force force = vForces.get(i);
			addForceDrawable(vRes, ptDepart, force, width, color);
		}
	}
	private void addForceDrawable (ArrayList vRes, Point_d ptDepart, Force force, float width, Color color) {
		if (force.dx==0 && force.dy==0) return;
		vRes.add (new ForceDrawable(ptDepart, force, width, color));
	}

}
